package com.iweb.oasys.servlet;

import com.iweb.oasys.service.MenuService;
import com.iweb.oasys.service.MenuServiceImpl;
import com.iweb.oasys.util.ResponseBody;
import com.iweb.oasys.util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "MenuServlet",urlPatterns = "/menu")
public class MenuServlet extends HttpServlet {

    private MenuService menuService = MenuServiceImpl.getInstance();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        ResponseBody responseBody = new ResponseBody();
        try {
            switch (action) {
                case "list":
                    responseBody = menuService.queryMenuList();
                    break;
                case "version": // 约定case = "version" 表示查询菜单的版本
                    responseBody = menuService.queryVersion();
                    break;
                default:
                    break;
            }
        }catch (Exception e) {
            e.printStackTrace();
            responseBody.setResCode(1);
            responseBody.setErrMsg(e.getMessage());
        }
        ServletUtil.returnResponse(response,responseBody);

    }
}
